package com.qding.park.common.core.redis.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

public class JedisClientSingle implements JedisClient {

	public static final String LOCKED = "TRUE";

	@Autowired
	private JedisPool jedisPool;

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();

		try {
			String string = jedis.get(key);

			return string;
		} finally {
			jedis.close();
		}

	}

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			String string = jedis.set(key, value);
			// jedis.close();
			return string;
		} finally {
			jedis.close();
		}
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			String string = jedis.hget(hkey, key);
			return string;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.hset(hkey, key, value);

			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.incr(key);

			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.expire(key, second);
			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.ttl(key);

			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long del(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.del(key);

			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long hdel(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			Long result = jedis.hdel(hkey, key);

			// jedis.brpop(timeout, key);
			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public Object get(String key, Class<?> objType) {
		Jedis jedis = jedisPool.getResource();
		try {
			String string = jedis.get(key);

			return StringUtils.isBlank(string) ? null : SerializeUtils.unSerialize(string, objType);
		} finally {
			jedis.close();
		}
	}

	// @Override
	// public String set(String key, Object value) {
	// Jedis jedis = jedisPool.getResource();
	// try {
	//// String string = jedis.set(key, value == null ? "" :
	// SerializeUtils.serialize(value));
	// String string = jedis.set(key, value);
	// return string;
	// } finally {
	// jedis.close();
	// }
	// }

	@Override
	public String set(String key, String value, int ttl) {
		if (StringUtils.isBlank(value)) {
			return null;
		}
		String string = this.set(key, value);
		if (-1 != ttl) {
			this.expire(key, ttl);
		}
		return string;
	}

	// @Override
	// public String set(String key, Object value, int ttl) {
	// // TODO Auto-generated method stub
	// String string = this.set(key, value);
	// this.expire(key, ttl);
	// return string;
	// }

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see
	 * com.qding.park.common.core.redis.JedisClient#publish(java.lang.String,
	 * java.lang.String) 向主题发布 消息
	 */
	@Override
	public long publish(String channel, String message) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();
		try {
			long result = jedis.publish(channel, message);

			return result;
		} finally {
			jedis.close();
		}
	}

	@Override
	public void subscribe(JedisPubSub jedisPubSub, final String... channels) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.subscribe(jedisPubSub, channels);

		} finally {
			jedis.close();
		}
	}

	@Override
	public long lpush(String key, String... string) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.lpush(key, string);
		} finally {
			jedis.close();
		}
	}

	@Override
	public String rpop(String key) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();
		try {
			String value = jedis.rpop(key);
			return value;
		} finally {
			jedis.close();
		}
	}

	@Override
	public List<String> brpop(String key) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();
		try {
			List<String> values = jedis.brpop(key);
			return values;
		} finally {
			jedis.close();
		}
	}

	@Override
	public long setnx(String lockKey, int expireTime, String stringOfLockExpireTime) {
		// TODO Auto-generated method stub
		Jedis jedis = jedisPool.getResource();

		try {
			long i = jedis.setnx(lockKey, stringOfLockExpireTime);
			if (i == 1) {
				// 加锁成功
				jedis.expire(lockKey, expireTime);
			}
			return i;
		} finally {
			jedis.close();
		}
	}

	@Override
	public String getSet(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.getSet(key, value);
		} finally {
			jedis.close();
		}
	}

	@Override
	public long setnx(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.setnx(key, value);
		} finally {
			jedis.close();
		}
	}

	@Override
	public Map<String, String> hgetAll(String hkey) {
		Jedis jedis = jedisPool.getResource();

		try {
			return jedis.hgetAll(hkey);
		} finally {
			jedis.close();
		}
	}

	@Override
	public void delPattern(String key) {

		Jedis jedis = jedisPool.getResource();

		try {
			Set<String> keySet=jedis.keys(key);
			for(String ss:keySet){
				jedis.del(ss);
			}
		} finally {
			jedis.close();
		}
	}

}
